package com.ruoyi.Test;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.WXConstants;
import com.ruoyi.project.order.domain.Customer;
import com.ruoyi.project.order.domain.WorkOrder;
import com.ruoyi.project.order.mapper.WorkOrderMapper;
import com.ruoyi.project.order.service.ICustomerService;
import com.ruoyi.project.order.service.IOrderTypeService;
import com.ruoyi.project.order.service.IWorkOrderService;
import com.ruoyi.project.order.vo.OrderFanKuiVo;
import com.ruoyi.project.order.vo.OrderListVo;
import com.ruoyi.project.order.vo.SelectOrderVo;
import com.ruoyi.project.order.wx.TemplateId;
import com.ruoyi.project.order.wx.WxSender;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MyTest {
    @Autowired
    ICustomerService iCustomerService;
    @Autowired
    IWorkOrderService iWorkOrderService;
    @Autowired
    IOrderTypeService iOrderTypeService;
    @Autowired
    WorkOrderMapper workOrderMapper;

    @Before
    public void before() {
        String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+WXConstants.AppID+"&secret="+WXConstants.AppSecret);
        if (result.contains("errcode")) {
            System.out.println("获取失败: "+result);
        }else{
            String access_token = JSON.parseObject(result).getString("access_token");
            log.info("获取成功!!: "+access_token);
            WXConstants.access_token = access_token;
        }
    }

    @Test
    public void test1() {
        Customer customer = new Customer();
        customer.setId(2);
        List<Customer> list = iCustomerService.list(new QueryWrapper<Customer>().setEntity(customer));
        list.forEach(System.out::println);
    }

    @Test
    public void testOrderListReturn() {
        List<WorkOrder> workOrders = iWorkOrderService.list();
        List<OrderListVo> orderListVos = new ArrayList<>();
        for (WorkOrder workOrder : workOrders) {
            OrderListVo orderListVo = new OrderListVo();
            BeanUtil.copyProperties(workOrder, orderListVo, false);
        }
    }

    @Test
    public void testListAll() {
        log.error("--------------------------------------------------------------\n\n\n\n\n");

        List<OrderListVo> orderListVo = workOrderMapper.LGetUserOrderNum();
        for (OrderListVo listVo : orderListVo) {
            log.info(listVo.getWorker() + "--------------" + listVo.getOrderNum());
        }
        log.error("\n\n\n\n\n--------------------------------------------------------------");
    }

    @Test
    public void genData() {
        //生成客户
        System.out.println("-----------------------------------------------------------------");
        System.out.println(RandomValue.getChineseName());
        System.out.println(RandomValue.getRoad());
        System.out.println(RandomValue.getTel());
        for (int i = 0; i < 233; i++) {
            Customer customer = new Customer();
            customer.setCustomerPhone(RandomValue.getTel());
            customer.setCustomerName(RandomValue.getChineseName());
            customer.setCustomerAddress(RandomValue.getRoad());
            iCustomerService.save(customer);

            WorkOrder workOrder = new WorkOrder();
            workOrder.setWorkerId(RandomUtil.randomInt(4, 7));
            workOrder.setPublisherId(3);
            workOrder.setCustomerId(RandomUtil.randomInt(1, 233));
            workOrder.setOrderCreateTime(LocalDateTime.now());
            workOrder.setOrderStartTime(DateUtil.toLocalDateTime(RandomUtil.randomDate(new Date(), DateField.HOUR_OF_DAY, 0, 100)));
            workOrder.setOrderEndTime(DateUtil.toLocalDateTime(RandomUtil.randomDate(new Date(), DateField.HOUR_OF_DAY, 150, 1000)));
            workOrder.setOrderType(RandomUtil.randomInt(1, 8));
            workOrder.setOrderTitle(RandomValue.getChineseName() + "的订单");
            workOrder.setOrderMoney(RandomUtil.randomDouble(0, 1000));
            workOrder.setOrderContent("去" + RandomValue.getChineseName() + "做一些事情...");
            iWorkOrderService.save(workOrder);
            System.out.println(customer);
            System.out.println(workOrder);
        }
    }

    /**
     * 测试模板消息
     */
    @Test
    public void testSender() {
        WxSender.sendMsg(8 , TemplateId.newWorkOrderTip,
                "你收到一条新的工单"
                , "修理东西", "111", "工单内容", "发起人1", "15215151515", "尽快完成"
        );
    }

    @Test
    public void orderFankui() {
        List<OrderFanKuiVo> orderFanKuiVos = workOrderMapper
                .LGetFanKui(247);
        System.out.println(orderFanKuiVos);
    }

    @Test
    public void 逾期() {
        List<OrderListVo> orderListVos = workOrderMapper.LGetListNoContentAndYuQi(new SelectOrderVo());
        for (OrderListVo orderListVo : orderListVos) {
            System.out.println(orderListVo);
        }
    }

    @Test
    public void 无接单者的单子() {
        List<OrderListVo> orderListVos = workOrderMapper.LGetListJieDan(new SelectOrderVo());
        for (OrderListVo orderListVo : orderListVos) {
            System.out.println(orderListVo);
        }
    }
}
